/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Login.java
 *
 * Created on 29-oct-2009, 8:25:43
 */

package scapi.gui;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import scapi.servidor.servidores.ServidorAUsuario;
import scapi.servidor.servidores.ServidorSessiones;
import scapi.utilerias.ServidorFactory;
import scapi.utilerias.SesionManager;

/**
 *
 * @author 5IM8
 */
public class Login extends javax.swing.JFrame {
    private ServidorAUsuario servUsuario;
    private ServidorSessiones servSession;

    /** Creates new form Login */
    public Login() {
        initComponents();

        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

        setLocation(screenSize.width/2 - (432/2),
                    screenSize.height/2-(153/2));
        try {
            servUsuario = (ServidorAUsuario) ServidorFactory.obtenerServidor(ServidorFactory.ServidorUsuario);
            servSession = (ServidorSessiones) ServidorFactory.obtenerServidor(ServidorFactory.ServidorSesion);

        } catch (RemoteException ex) {
            JOptionPane.showMessageDialog(this,"No se puede conectar al servidor", "ERROR", JOptionPane.ERROR_MESSAGE);
            ex.printStackTrace();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        JLLIuser = new javax.swing.JLabel();
        JLLIpass = new javax.swing.JLabel();
        JLLIerror = new javax.swing.JLabel();
        TFLIuser = new javax.swing.JTextField();
        PFLIpass = new javax.swing.JPasswordField();
        JBLIcancelar = new javax.swing.JButton();
        JBLIlogin = new javax.swing.JButton();
        JLLIimagen = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Iniciar sesion");
        setResizable(false);

        JLLIuser.setText("Usuario:");

        JLLIpass.setText("Contraseña:");

        JLLIerror.setFont(new java.awt.Font("Tahoma", 2, 10));
        JLLIerror.setForeground(new java.awt.Color(255, 0, 0));
        JLLIerror.setText(" ");

        JBLIcancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/scapi/imagenes/no_16.png"))); // NOI18N
        JBLIcancelar.setText("Cancelar");
        JBLIcancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JBLIcancelarActionPerformed(evt);
            }
        });

        JBLIlogin.setIcon(new javax.swing.ImageIcon(getClass().getResource("/scapi/imagenes/log.png"))); // NOI18N
        JBLIlogin.setText("Iniciar Sesion");
        JBLIlogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JBLIloginActionPerformed(evt);
            }
        });

        JLLIimagen.setIcon(new javax.swing.ImageIcon(getClass().getResource("/scapi/imagenes/login6.png"))); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(JLLIimagen)
                .addGap(10, 10, 10)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(JLLIpass)
                            .addComponent(JLLIuser))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(TFLIuser, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(PFLIpass, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(JBLIlogin, javax.swing.GroupLayout.PREFERRED_SIZE, 133, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(JBLIcancelar, javax.swing.GroupLayout.DEFAULT_SIZE, 119, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(JLLIerror, javax.swing.GroupLayout.PREFERRED_SIZE, 256, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(TFLIuser, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(JLLIuser))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(PFLIpass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(JLLIpass))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(JLLIerror)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(JBLIlogin)
                            .addComponent(JBLIcancelar)))
                    .addComponent(JLLIimagen))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void JBLIloginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JBLIloginActionPerformed

        String password = new String(PFLIpass.getPassword());
        String usuario = TFLIuser.getText();
        try {
            String[] respuesta = servUsuario.consultaPass(usuario);
            String pass = respuesta[0];
            String tipo = respuesta[1];

            if(password.equals(pass)){
                SesionManager.printIDSession(usuario);
                servSession.createSession(SesionManager.getIDSession());

                if(tipo.equals("Registrador")){
                    Registrador.iniciar();
                    this.dispose();

                }else if(tipo.equals("Valuador")){
                    Valuador2.iniciar();
                    this.dispose();

                }else if(tipo.equals("Cajero")){
                    GUICajero.iniciar();
                    this.dispose();

                }else if(tipo.equals("Administrador")){
                    GUIAdministrador.iniciar();
                    this.dispose();
                    
                }
                
            }else{
                JLLIerror.setText("nombre de usuario y/o password no valido");
                pack();
            }


        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }//GEN-LAST:event_JBLIloginActionPerformed

    private void JBLIcancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JBLIcancelarActionPerformed

        System.exit(0);
        // TODO add your handling code here:
    }//GEN-LAST:event_JBLIcancelarActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void iniciar() {

        try {
           // UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (Exception e) {
            //Just print stacktrace here since it's an example.
            e.printStackTrace();
        }

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Login().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton JBLIcancelar;
    private javax.swing.JButton JBLIlogin;
    private javax.swing.JLabel JLLIerror;
    private javax.swing.JLabel JLLIimagen;
    private javax.swing.JLabel JLLIpass;
    private javax.swing.JLabel JLLIuser;
    private javax.swing.JPasswordField PFLIpass;
    private javax.swing.JTextField TFLIuser;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables

}
